package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/6 11:18 下午
 */
public class 剑指_Offer_24_反转链表 {

    /**
     * 三指针，反转链表
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return head;
        }

        ListNode fakeHead = new ListNode(-1);
        fakeHead.next = head;
        ListNode prePointer, currentPointer, postPointer;
        prePointer = fakeHead;
        currentPointer = fakeHead.next;
        postPointer = fakeHead.next.next;
        while (currentPointer != null) {
            currentPointer.next = prePointer;
            prePointer = currentPointer;
            currentPointer = postPointer;
            if (postPointer != null) {
                postPointer = postPointer.next;
            }
        }
        fakeHead.next.next = null;
        return prePointer;
    }
}
